//
//  HZViewController.m
//  XTXOneToOne
//
//  Created by Canny on 2018/11/5.
//  Copyright © 2018年 WK. All rights reserved.
//

#import "HZViewController.h"

@interface HZViewController ()

@property (nonatomic,strong)UILabel *lab3;
@property (nonatomic,strong)UILabel *lab4;
@property (nonatomic,strong)UITextField * textField1;
@property (nonatomic,strong)UITextField * textField2;
@property (nonatomic,strong)NSString * max;

@end

@implementation HZViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    [self setProperty];
    [self createUI];
    [self assets_currency:YES];
    [self leverassets_currency:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBarHidden=YES;

}

-(void)setProperty{
    
    self.view.backgroundColor=RGB(255, 255, 255);
    _max=@"0";
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBarHidden=YES;
    
    if ([_Type isEqualToString:@"币币资产"]) {
        self.baseVcTitle = [NSString stringWithFormat:@"%@划转",_BBmodel.symbol];
    }else if ([_Type isEqualToString:@"杠杆资产"]){
        self.baseVcTitle = [NSString stringWithFormat:@"%@划转",_GGmodel.symbol];
    }

}


#pragma mark-BB
-(void)assets_currency:(BOOL)isShow{
    NSString * URL = [NSString stringWithFormat:@"%@assets/currency",DEFAULTSEVERBASEURL];
    NSString *currency_id;
    if ([_Type isEqualToString:@"币币资产"]) {
        currency_id=_BBmodel.currency_id;
    }else if ([_Type isEqualToString:@"杠杆资产"]){
        currency_id=_GGmodel.currency_id;
    }
    NSDictionary * Dic =@{
                          @"currency_id":currency_id,
                          @"access_token":[LoginModel GetValue:DL_token]
                          };
    if (isShow) {
        [HUD showProgress:@"" inView:self.bgView];
    }
    WS(weakSelf);
    
    [MOLoadHttpsData PostHttpDataWithUrlStr:URL Dic:Dic SuccessBlock:^(id  _Nonnull responseObject) {
        if (isShow) {
            [HUD dismissInView:weakSelf.bgView];
        }
        
        NSDictionary * dic = responseObject;
        
        NSString * code = [NSString stringWithFormat:@"%@",dic[@"code"]];
//        NSString * msg = [NSString stringWithFormat:@"%@",dic[@"msg"]];
        
        if ([code isEqualToString:@"0"]) {
            
            NSDictionary * dicc = dic[@"data"];
            NSString * available = dicc[@"available"];
            if ([weakSelf.Type isEqualToString:@"币币资产"]) {

                weakSelf.lab3.text = [NSString stringWithFormat:@"%@ %@",available,weakSelf.BBmodel.symbol];
                weakSelf.max =[NSString stringWithFormat:@"%@",available];
                
            }else if ([weakSelf.Type isEqualToString:@"杠杆资产"]){

                weakSelf.lab4.text = [NSString stringWithFormat:@"%@ %@",available,weakSelf.GGmodel.symbol];

            }
            
        }
        
        
    } FailureBlock:^(id  _Nonnull error) {
        if (isShow) {
            [HUD dismissInView:weakSelf.bgView];
        }
        
    }];
    
}

#pragma mark-GG
-(void)leverassets_currency:(BOOL)isShow{
    NSString * URL = [NSString stringWithFormat:@"%@leverassets/currency",DEFAULTSEVERBASEURL];
    NSString *currency_id;
    if ([_Type isEqualToString:@"币币资产"]) {
        currency_id=_BBmodel.currency_id;
    }else if ([_Type isEqualToString:@"杠杆资产"]){
        currency_id=_GGmodel.currency_id;
    }
    NSDictionary * Dic =@{
                          @"currency_id":currency_id
                          };
 
    if (isShow) {
        [HUD showProgress:@"" inView:self.bgView];

    }
    WS(weakSelf);
    
    [MOLoadHttpsData GetHttpDataWithUrlStr:URL Dic:Dic SuccessBlock:^(id  _Nonnull responseObject) {
        if (isShow) {
            [HUD dismissInView:weakSelf.bgView];
        }

        NSDictionary * dic = responseObject;
        
        NSString * code = [NSString stringWithFormat:@"%@",dic[@"code"]];
//        NSString * msg = [NSString stringWithFormat:@"%@",dic[@"msg"]];
        
        if ([code isEqualToString:@"0"]) {
            
            NSDictionary * dicc = dic[@"data"];
            NSString * available = dicc[@"available"];
            if ([weakSelf.Type isEqualToString:@"币币资产"]) {
                
                weakSelf.lab4.text = [NSString stringWithFormat:@"%@ %@",available,weakSelf.BBmodel.symbol];
                
            }else if ([weakSelf.Type isEqualToString:@"杠杆资产"]){
                
                weakSelf.lab3.text = [NSString stringWithFormat:@"%@ %@",available,weakSelf.GGmodel.symbol];
                weakSelf.max =[NSString stringWithFormat:@"%@",available];
            }
            
            
        }
        
        
    } FailureBlock:^(id  _Nonnull error) {
        if (isShow) {
            [HUD dismissInView:weakSelf.bgView];
        }
    }];
    
}

-(void)createUI{
    NSString * str_from =@"", *str_to=@"";
    if ([_Type isEqualToString:@"币币资产"]) {
        str_from=@"从 法币交易";
        str_to=@"到 杠杆交易";
    }else if ([_Type isEqualToString:@"杠杆资产"]){
        str_from=@"从 法币交易";
        str_to=@"到 杠杆交易";
    }
    UILabel * lab1 = [[UILabel alloc]init];
    [self.bgView addSubview:lab1];
    [self setLab:lab1 withColor:RGB(30, 63, 89) andFont:15 andText:str_from andTextAlignment:NSTextAlignmentLeft];
    [lab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_top).offset(50);
        make.left.equalTo(self.bgView.mas_left).offset(15);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(30);
    }];
    
    UIImageView * imgV = [[UIImageView alloc]init];
    [self.bgView addSubview:imgV];
    imgV.image=[UIImage imageNamed:@"互转icon"];
    [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.bgView.mas_centerX).offset(0);
        make.centerY.equalTo(lab1.mas_centerY).offset(0);        make.width.mas_equalTo(20);
        make.height.mas_equalTo(17);
    }];
    
    UILabel * lab3 = [[UILabel alloc]init];
    [self.bgView addSubview:lab3];
    [self setLab:lab3 withColor:RGB(169, 174, 192) andFont:12 andText:@"0" andTextAlignment:NSTextAlignmentLeft];
    [lab3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lab1.mas_bottom).offset(0);
        make.left.equalTo(self.bgView.mas_left).offset(15);
        make.width.mas_equalTo(250);
        make.height.mas_equalTo(20);
    }];
    _lab3 = lab3;
    
    UILabel * lab2 = [[UILabel alloc]init];
    [self.bgView addSubview:lab2];
    [self setLab:lab2 withColor:RGB(30, 63, 89) andFont:15 andText:str_to andTextAlignment:NSTextAlignmentRight];
    [lab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_top).offset(50);
        make.right.equalTo(self.bgView.mas_right).offset(-15);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(30);
    }];
    
    UILabel * lab4 = [[UILabel alloc]init];
    [self.bgView addSubview:lab4];
    [self setLab:lab4 withColor:RGB(169, 174, 192) andFont:12 andText:@"0" andTextAlignment:NSTextAlignmentRight];
    [lab4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lab2.mas_bottom).offset(0);
        make.right.equalTo(self.bgView.mas_right).offset(-15);
        make.width.mas_equalTo(250);
        make.height.mas_equalTo(20);
    }];
    _lab4 = lab4;
    
    UILabel * lab5 = [[UILabel alloc]init];
    [self.bgView addSubview:lab5];
    [self setLab:lab5 withColor:RGB(30, 63, 89) andFont:12 andText:@"划转数量(0手续费)" andTextAlignment:NSTextAlignmentLeft];
    [lab5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lab3.mas_bottom).offset(30);
        make.left.equalTo(self.bgView.mas_left).offset(15);
        make.width.mas_equalTo(250);
        make.height.mas_equalTo(20);
    }];
    
    UITextField * textField1=[[UITextField alloc]init];
    [self.bgView addSubview:textField1];
    [self setTextField:textField1 WithPlaceholder:@"请输入划转数量" and:UIKeyboardTypeDecimalPad];
    textField1.backgroundColor = [UIColor clearColor];
    [textField1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(15);
        make.right.equalTo(self.bgView.mas_right).offset(-120);
        make.height.mas_equalTo(40);
        make.top.equalTo(lab5.mas_bottom).offset(20);
    }];
    _textField1=textField1;
    
    UIView * line1 = [[UIView alloc]init];
    line1.backgroundColor=RGB(230, 234, 237);
    [self.bgView addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(15);
        make.right.equalTo(self.bgView.mas_right).offset(-15);
        make.height.mas_equalTo(1);
        make.top.equalTo(textField1.mas_bottom).offset(0);
    }];
    
    UIButton *  bugAll = [[UIButton alloc]init];
    [self.bgView addSubview:bugAll];
    [bugAll setTitleColor:ThemeColor forState:UIControlStateNormal];
    [bugAll setTitle:@"全部划转" forState:UIControlStateNormal];
    bugAll.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    bugAll.titleLabel.font = [UIFont systemFontOfSize: 15.0];
    [bugAll addTarget:self action:@selector(buyAllClick) forControlEvents:UIControlEventTouchUpInside];
    [bugAll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(textField1.mas_centerY).offset(0);
        make.right.equalTo(self.bgView.mas_right).offset(-15);
        make.width.mas_equalTo(85);
        make.height.mas_equalTo(40);
    }];
    

    UITextField * textField2=[[UITextField alloc]init];
    [self.bgView addSubview:textField2];
    [self setTextField:textField2 WithPlaceholder:@"请输入资金密码" and:UIKeyboardTypeDefault];
    textField2.backgroundColor = [UIColor clearColor];
    [textField2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(15);
        make.right.equalTo(self.bgView.mas_right).offset(-120);
        make.height.mas_equalTo(40);
        make.top.equalTo(line1.mas_bottom).offset(10);
    }];
    textField2.secureTextEntry=YES;
     _textField2=textField2;


    UIView * line2 = [[UIView alloc]init];
    line2.backgroundColor=RGB(230, 234, 237);
    [self.bgView addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(15);
        make.right.equalTo(self.bgView.mas_right).offset(-15);
        make.height.mas_equalTo(1);
        make.top.equalTo(textField2.mas_bottom).offset(0);
    }];

    UIButton *  ok = [[UIButton alloc]init];
    [self.bgView addSubview:ok];
    [ok setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [ok setTitle:@"确定" forState:UIControlStateNormal];
    ok.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    ok.titleLabel.font = [UIFont systemFontOfSize: 15.0];
    [ok addTarget:self action:@selector(okClick) forControlEvents:UIControlEventTouchUpInside];
    ok.backgroundColor=ThemeColor;
    [ok mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line2.mas_bottom).offset(30);
        make.right.equalTo(self.bgView.mas_right).offset(-15);
        make.left.equalTo(self.bgView.mas_left).offset(15);
        make.height.mas_equalTo(45);
    }];
    
    
}

-(void)okClick{
    
    if (_textField1.text.length<1) {
        [HUD showErrorMessage:@"请输入划转数量！"];
        return;
    }
    if (_textField2.text.length<1) {
        [HUD showErrorMessage:@"请输入资金密码！"];
        return;
    }
    //资金划转操作（币币账户到杠杆账户）
    if ([_Type isEqualToString:@"币币资产"]) {

        NSString * URL = [NSString stringWithFormat:@"%@assets/recharge",DEFAULTSEVERBASEURL];
        
        NSDictionary * Dic =@{
                              @"currency_id":_BBmodel.currency_id,
                              @"amount":_textField1.text,
                              @"asset_pwd":_textField2.text,
                              @"access_token":[LoginModel GetValue:DL_token]
                              };
        [HUD showProgress:@"" inView:self.bgView];
        WS(weakSelf);
        [MOLoadHttpsData PostHttpDataWithUrlStr:URL Dic:Dic SuccessBlock:^(id  _Nonnull responseObject) {
            [HUD dismissInView:weakSelf.bgView];
            
            NSDictionary * dic = responseObject;
            
            NSString * code = [NSString stringWithFormat:@"%@",dic[@"code"]];
            NSString * msg = [NSString stringWithFormat:@"%@",dic[@"msg"]];
            if ([code isEqualToString:@"0"]) {
                weakSelf.textField1.text = @"";
                weakSelf.textField2.text = @"";
                [HUD showErrorMessage:msg];
                [weakSelf assets_currency:NO];
                [weakSelf leverassets_currency:NO];
            }else{
                [HUD showErrorMessage:msg];
            }
            
            
        } FailureBlock:^(id  _Nonnull error) {
            [HUD dismissInView:weakSelf.bgView];
            
            
            
        }];
            
        
    //    资金划转操作（杠杆账户到币币账户）
    }else if ([_Type isEqualToString:@"杠杆资产"]){
   
        NSString * URL = [NSString stringWithFormat:@"%@leverassets/recharge",DEFAULTSEVERBASEURL];
        
        NSDictionary * Dic =@{
                              @"currency_id":_GGmodel.currency_id,
                              @"amount":_textField1.text,
                              @"asset_pwd":_textField2.text,
                              @"access_token":[LoginModel GetValue:DL_token]
                              };
        [HUD showProgress:@"" inView:self.bgView];
        WS(weakSelf);
        [MOLoadHttpsData PostHttpDataWithUrlStr:URL Dic:Dic SuccessBlock:^(id  _Nonnull responseObject) {
            [HUD dismissInView:weakSelf.bgView];
            
            
            NSDictionary * dic = responseObject;
            
            NSString * code = [NSString stringWithFormat:@"%@",dic[@"code"]];
            NSString * msg = [NSString stringWithFormat:@"%@",dic[@"msg"]];
            if ([code isEqualToString:@"0"]) {
                weakSelf.textField1.text = @"";
                weakSelf.textField2.text = @"";
                [HUD showErrorMessage:msg];
                [weakSelf assets_currency:NO];
                [weakSelf leverassets_currency:NO];
            }else{
                [HUD showErrorMessage:msg];
            }
            
            
        } FailureBlock:^(id  _Nonnull error) {
            [HUD dismissInView:weakSelf.bgView];
            
            
            
        }];
        
    }
    
}


-(void)buyAllClick{
    _textField1.text = _max;
    
}

-(void)setLab:(UILabel *)lab withColor:(UIColor *)color andFont:(CGFloat)font andText:(NSString *)text andTextAlignment:(NSTextAlignment)textAlignment{
    lab.text=text;
    lab.textColor=color;
    lab.font=[UIFont systemFontOfSize:font];
    lab.textAlignment=textAlignment;
}


-(void)setTextField:(UITextField*)textField WithPlaceholder:(NSString *)placeholder and:(UIKeyboardType)type  {
    
    
    textField.placeholder=placeholder;
    textField.borderStyle = UITextBorderStyleNone;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    //    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    textField.backgroundColor=[UIColor clearColor];
    textField.font = [UIFont systemFontOfSize:15];
    textField.textColor = RGB(32, 64, 89);
    textField.textAlignment = NSTextAlignmentLeft;
    //    textField.returnKeyType = UIReturnKeyDefault;
    textField.keyboardType = type;
    [textField setValue:RGB(190, 190, 190) forKeyPath:@"_placeholderLabel.textColor"];
    [textField setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:(UIControlEventEditingChanged)];
    
}

-(void)textFieldDidChange:(UITextField *)textField{
    textField.text= [textField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    if (textField == _textField1) {
        if ([textField.text doubleValue]>[_max doubleValue]) {
            textField.text=_max;
        }
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
